package jgl.vajra.management.part.home.pop;

import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import jgl.vajra.management.R;
import jgl.vajra.management.bean.AccountingMethodBean;
import jgl.vajra.management.databinding.ItemAccountingMethodBinding;
import jgl.vajra.management.databinding.PopAccountingMethodBinding;
import jgl.vajra.management.widget.databindingadapter.BaseBindingItemPresenter;
import jgl.vajra.management.widget.databindingadapter.BaseDataBindingDecorator;
import jgl.vajra.management.widget.databindingadapter.SingleTypeBindingAdapter;
import jgl.vajra.management.widget.utils.UIUtils;

public class AccountingMethodPop implements BaseBindingItemPresenter<AccountingMethodBean.AccountingMethodTypeBean> {

    private FragmentActivity activity;
    private View mPopRootView;
    private PopupWindow menuPop;
    private AccountingMethodListener accountingMethodListener;

    public AccountingMethodPop(FragmentActivity activity) {

        this.activity = activity;
    }


    private void initPopupWindow() {
        mPopRootView = View.inflate(UIUtils.getContext(), R.layout.pop_accounting_method, null);
        PopAccountingMethodBinding mBinding = DataBindingUtil.bind(mPopRootView);

        menuPop = new PopupWindow(mPopRootView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        menuPop.setOutsideTouchable(false);
        menuPop.setBackgroundDrawable(new BitmapDrawable());
        menuPop.setFocusable(true);// 点击空白处时，隐藏掉pop窗口
        menuPop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                //popupwindow消失的时候恢复成原来的透明度
                backgroundAlpha(1f);
            }
        });
        mBinding.setView(this);


        //机械类型
        // 不可嵌套滑动
        mBinding.recyclerView.setNestedScrollingEnabled(false);
        // 取消head 的 recycleView的焦点
        mBinding.recyclerView.setFocusableInTouchMode(false);
        //设置布局管理器
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        SingleTypeBindingAdapter adapter = new SingleTypeBindingAdapter(activity, AccountingMethodBean.getListData(), R.layout.item_accounting_method);
        mBinding.recyclerView.setAdapter(adapter);
        adapter.setItemPresenter(this);
        adapter.setItemDecorator((BaseDataBindingDecorator<AccountingMethodBean, ItemAccountingMethodBinding>) (binding, position, viewType, data) -> {

            if (position == 2) {
                binding.viewLine.setVisibility(View.GONE);
            }

            //机械类型
            // 不可嵌套滑动
            binding.itemRecyclerView.setNestedScrollingEnabled(false);
            // 取消head 的 recycleView的焦点
            binding.itemRecyclerView.setFocusableInTouchMode(false);
            //设置布局管理器
            binding.itemRecyclerView.setLayoutManager(new GridLayoutManager(activity, 4));
            SingleTypeBindingAdapter itemAdapter = new SingleTypeBindingAdapter(activity, data.typeList, R.layout.item_accounting_method_item);
            binding.itemRecyclerView.setAdapter(itemAdapter);
            itemAdapter.setItemPresenter(this);

        });
    }

    public void showPopupWindow(View v) {
        if (menuPop == null)
            initPopupWindow();
        int[] location = new int[2];
        v.getLocationOnScreen(location);  //获取控件的位置坐标
        //获取自身的长宽高
        mPopRootView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        backgroundAlpha(0.5f);

//        menuPop.showAtLocation(v, Gravity.NO_GRAVITY, (int) (location[0] - DisplayUtils.getDimension(R.dimen.dp_4)), (int) (location[1] - mPopRootView.getMeasuredHeight() - DisplayUtils.getDimension(R.dimen.dp_4)));  //显示指定控件的上方
//        menuPop.showAsDropDown(v, 0, 0);    //显示指定控件的下方

        menuPop.showAtLocation(v, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    public boolean isShowing() {
        return menuPop != null && menuPop.isShowing();
    }

    public void dismiss() {
        if (menuPop != null && menuPop.isShowing())
            menuPop.dismiss();
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        if (activity == null) return;
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        activity.getWindow().setAttributes(lp);

    }

    @Override
    public void onItemClick(int position, AccountingMethodBean.AccountingMethodTypeBean itemData) {
        if (accountingMethodListener != null) {
            accountingMethodListener.onDetermine(itemData.title);
        }
        dismiss();
    }

    public void setAccountingMethodListener(AccountingMethodListener listener) {
        accountingMethodListener = listener;

    }

    public interface AccountingMethodListener {
        void onDetermine(String itemData);
    }
}
